//
//  FTCodeTextField.m
//  FileToolkit
//
//  Created by Duncan on 2021/2/4.
//

#import "AICodeTextField.h"

@implementation AICodeTextField

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.timeCount = 60;
        
        self.placeholder = @"请输入验证码";
        self.keyboardType = UIKeyboardTypeNumberPad;
        self.background = [UIImage kj_imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)];
        
        
        [self.leftBackView addSubview:self.leftImageView];
        self.leftView = self.leftBackView;
        self.leftViewMode = UITextFieldViewModeAlways;
        
        [self.rightBackView addSubview:self.codeButton];
        self.rightView = self.rightBackView;
        self.rightViewMode = UITextFieldViewModeAlways;
        
        [self addSubview:self.lineView];
        [self masonryLayout];
    }
    return self;
}

- (void)sendCodeSuccess {
    self.codeButton.enabled = NO;
    self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(time:) userInfo:nil repeats:YES];
}

- (void)deallocView {
    if (self.time) {
        [self.time invalidate];
    }
}

- (void)dealloc {
    
}

#pragma mark ------- Private Methods -------

- (void)time:(NSTimer *)time {
    
    self.timeCount --;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%lds",(long)self.timeCount] forState:UIControlStateNormal];
    
    if (self.timeCount == -1) {
        
        self.timeCount = 60;
        
        if ([self.time isValid]) {
            
            [self.time invalidate];
        }
        self.time = nil;
        
        self.codeButton.enabled = YES;
        [self.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
    }
}

#pragma mark ------- Lazy Methods -------

- (void)masonryLayout {
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
}

- (UIView *)leftBackView {
    if (!_leftBackView) {
        _leftBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kFragmemtFloat(25), kFragmemtFloat(50))];
    }
    return _leftBackView;
}

- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithImage:@"icon_code".image];
        _leftImageView.frame = CGRectMake(kFragmemtFloat(5), kFragmemtFloat(18), kFragmemtFloat(10), kFragmemtFloat(12));
    }
    return _leftImageView;
}

- (UIView *)rightBackView {
    if (!_rightBackView) {
        _rightBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kFragmemtFloat(95), kFragmemtFloat(50))];
    }
    return _rightBackView;
}

- (UIButton *)codeButton {
    if (!_codeButton) {
        _codeButton = [UIButton initWithTitle:@"获取验证码" titleColor:[UIColor whiteColor] fontSize:12];
        _codeButton.backgroundColor = k_Color_themeColor;
        _codeButton.cornerRadius = kFragmemtFloat(5);
        _codeButton.frame = CGRectMake(kFragmemtFloat(5), kFragmemtFloat(5), kFragmemtFloat(90), kFragmemtFloat(32));
    }
    return _codeButton;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = k_Color_LineColor;
    }
    return _lineView;
}

@end
